// 自定义函数-常用工具
import { reactive, ref } from "vue";
export function usePagination(
  loadData: () => Promise<any>,
  initalPageSize = 10
) {
  // 声明总条数
  const totals = ref<any>(0);

  // 声明页码
  const pageInfo = reactive({
    page: 1,
    pageSize: initalPageSize,
  });

  const handleSizeChange = (size: number) => {
    pageInfo.pageSize = size;
    loadData(); // 重新加载数据
  };

  // 处理当前页码变化
  const handleCurrentChange = (page: number) => {
    pageInfo.page = page;
    loadData(); // 重新加载数据
  };

  // 对外暴露分页重置方法
  const resetPagination = () => {
    pageInfo.page = 1;
    pageInfo.pageSize = initalPageSize;
  };

  // 设置总条数
  const setTotals = (count: number) => {
    totals.value = count;
  };

  return {
    totals,
    pageInfo,
    handleSizeChange,
    handleCurrentChange,
    resetPagination,
    setTotals,
  };
}
